const fp = require('lodash/fp')
const { Maybe, Container } = require('./support')



// 1、
let maybe = Maybe.of([5,6,1])

// let ex1 = x => {
// 	// 需要遍历再加1
// 	let fn = fp.map(fp.add(1))
// 	return fn(x)
// }

// 
// let ex1 = x => fp.map(fp.add(1))(x)
let ex1 = () => maybe.map(fp.map(fp.add(1)))._value

console.log( maybe.map(ex1) )





// 2、
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])

// let ex2 = x => fp.first(x)
let ex2 = () => xs.map(fp.first)._value

console.log( xs.map(ex2) )





// 3、
let safeProp = fp.curry(function (x, o) {
	return Maybe.of(o[x])
})
let user = { id: 2, name: 'Albert'}

// console.log(safeProp('name')(user).map(fp.first))
// let ex3 = () => safeProp('name')(user).map(fp.first)

let u = Container.of(user)

// let ex3 = () => u.map(safeProp('name'))._value.map(fp.first)

// let ex3 = () => u.map(safeProp('name'))
// Container { _value: Maybe { _value: 'Albert' } }

// 函子是具有map方法的一个对象，在函子里面要维护一个值，这个值永远不对外公布，
// 就像这个值包裹在一个盒子里面，我们想要对这个值进行处理的话就调用map方法，通过map方法传递一个处理值的函数，map方法执行完后会返回一个新的函子，所以可以通过不停的 .map 去链式调用
let ex3 = () => u.map(safeProp('name')).map(x=>x._value).map(fp.first)
// Container { _value: 'A' }


console.log( ex3() )




// 4、
let ex4 = function (n) {
  return Maybe.of(n).map(parseInt)
}



console.log( ex4('7R') )   // Maybe { _value: 7 }
console.log( ex4('7.6B'))  // Maybe { _value: 7 }
console.log( ex4('8.2G') ) // Maybe { _value: 8 }

console.log( ex4(null) )      // Maybe { _value: null }
console.log( ex4(undefined) ) // Maybe { _value: undefined }

console.log( ex4('i7.5') )    // Maybe { _value: NaN }
console.log( ex4('abc') )     // Maybe { _value: NaN }

